package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.fragment.AlbumStatusFragment;
import com.github.postsanf.yinian.utils.SpUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

public class TimeLineActivity extends AppCompatActivity {
    private ImageView iv_back;
    private TextView tv_time_line;
    private RelativeLayout rl_lead;
    private boolean timeLineLead;
    private ImageView iv_skip;
    private ImageView iv_share_long;
    private ImageView iv_first_circle;
    private ImageView iv_share_all;
    private ImageView iv_skip_all;
    private ImageView iv_first_circle_all;
    private ImageView iv_share_qrcode;
    private ImageView iv_third_circle;
    private ImageView iv_skip2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_line);
        registBroadcastReceiver();
        initView();
    }

    private void registBroadcastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTIONs.lead);
        registerReceiver(MyReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(MyReceiver);
    }

    BroadcastReceiver MyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case ACTIONs.lead:
                    if (!timeLineLead) {
                        rl_lead.setVisibility(View.VISIBLE);
                        ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ctfx_xiayiye, iv_skip);
                        iv_skip.setVisibility(View.VISIBLE);
                        Glide.with(TimeLineActivity.this).load(R.drawable.ctfx_tupian).thumbnail(0.8f).into(iv_share_long);
                        iv_share_long.setVisibility(View.VISIBLE);
                        ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ctfx_tuoyuan, iv_first_circle);
                        iv_first_circle.setVisibility(View.VISIBLE);
                    } else {
                        rl_lead.setVisibility(View.GONE);
                    }
                    break;
            }
        }
    };


    private void initView() {
        iv_back = ((ImageView) findViewById(R.id.iv_back));
        tv_time_line = ((TextView) findViewById(R.id.tv_time_line));
        tv_time_line.setTypeface(Typeface.createFromAsset(getAssets(), "fzqk.ttf"));
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl_contaier, new AlbumStatusFragment()).commit();
        rl_lead = ((RelativeLayout) findViewById(R.id.rl_lead));
        timeLineLead = SpUtils.getBoolean(TimeLineActivity.this, "TimeLineLead");
        iv_skip = ((ImageView) findViewById(R.id.iv_skip));
        iv_share_long = ((ImageView) findViewById(R.id.iv_share_long));
        iv_first_circle = ((ImageView) findViewById(R.id.iv_first_circle));
        iv_share_all = ((ImageView) findViewById(R.id.iv_share_all));
        iv_skip_all = ((ImageView) findViewById(R.id.iv_skip_all));
        iv_first_circle_all = ((ImageView) findViewById(R.id.iv_first_circle_all));
        iv_share_qrcode = ((ImageView) findViewById(R.id.iv_share_qrcode));
        iv_third_circle = ((ImageView) findViewById(R.id.iv_third_circle));
        iv_skip2 = ((ImageView) findViewById(R.id.iv_skip2));
        iv_skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_skip.setVisibility(View.GONE);
                iv_share_long.setVisibility(View.GONE);
                iv_first_circle.setVisibility(View.GONE);
                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ctfx_xiayiye, iv_skip2);
                iv_skip2.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.lead_qrcode, iv_share_qrcode);
                iv_share_qrcode.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ewma_tuoyuan, iv_third_circle);
                iv_third_circle.setVisibility(View.VISIBLE);
            }
        });
        iv_skip2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_skip2.setVisibility(View.GONE);
                iv_share_long.setVisibility(View.GONE);
                iv_first_circle.setVisibility(View.GONE);
                iv_third_circle.setVisibility(View.GONE);
                iv_share_qrcode.setVisibility(View.GONE);
                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.cjkj_tiaoguo, iv_skip_all);
                iv_skip_all.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ztfx_tupian, iv_share_all);
                iv_share_all.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage("drawable://" + R.drawable.ztfx_tuoyuan, iv_first_circle_all);
                iv_first_circle_all.setVisibility(View.VISIBLE);
            }
        });
        iv_skip_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_skip_all.setVisibility(View.GONE);
                iv_share_all.setVisibility(View.GONE);
                iv_first_circle_all.setVisibility(View.GONE);
                rl_lead.setVisibility(View.GONE);
                timeLineLead = true;
                SpUtils.put(TimeLineActivity.this, "TimeLineLead", true);
//                sendBroadcast(new Intent(ACTIONs.timeLineLead));
            }
        });
    }

    private long fristTime;

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
        fristTime = System.currentTimeMillis();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
        Intent intent = new Intent(ACTIONs.record);
        intent.putExtra("key", "timeAxis_3");
        intent.putExtra("value", new Long(System.currentTimeMillis() - fristTime).intValue());
        sendBroadcast(intent);
    }
}
